//
//  PlayerSkinView.swift
//  GenericProjectForSwift
//
//  Created by 皮蛋菌 on 2023/5/14.
//  Copyright © 2023 漠然丶情到深处. All rights reserved.
//

import UIKit

class PlayerSkinView: UIView {
    var blackButton: UIButton?
    var titleLabel: UILabel?
    var timeLabel: UILabel?
    var durationLabel: UILabel?
    var progressSlider: UISlider?
    var playButton: UIButton?
    var rateButton: UIButton?
    var fullScreenButton: UIButton?
    
    var blackActionBlock: (() -> Void)?
    var progressChangeBlock: (() -> Void)?
    var playActionBlock: ((Bool) -> Void)?
    var rateActionBlock: ((UIButton) -> Void)?
    var fullScreenActionBlock: ((Bool) -> Void)?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func createSubViews() {
        var leftSafePadding: CGFloat = 0.0
        var rightSafePadding: CGFloat = 0.0
        var bottomSafePadding: CGFloat = 0.0
        if #available(iOS 11.0, *) {
            leftSafePadding = self.safeAreaInsets.left
            rightSafePadding = self.safeAreaInsets.right
            bottomSafePadding = self.safeAreaInsets.bottom
        }
        let screenWidth = UIScreen.main.bounds.size.width
        
        blackButton = UIButton.init()
        blackButton!.setImage(LSImageNamed(name: "back_white"), for: .normal)
        blackButton!.addTarget(self, action: #selector(blackAction), for: .touchUpInside)
        self.addSubview(blackButton!)

        titleLabel = UILabel.init()
        titleLabel!.textColor = LSWhiteColor
        self.addSubview(titleLabel!)

        timeLabel = UILabel.init()
        timeLabel!.text = "00:00"
        timeLabel!.textColor = LSWhiteColor
        timeLabel!.textAlignment = .center
        self.addSubview(timeLabel!)

        durationLabel = UILabel.init()
        durationLabel!.text = "00:00"
        durationLabel!.textColor = LSWhiteColor
        durationLabel!.textAlignment = .center
        self.addSubview(durationLabel!)

        progressSlider = UISlider.init()
        progressSlider!.minimumValue = 0
        progressSlider!.maximumValue = 1
        progressSlider!.addTarget(self, action: #selector(progressSliderValueChanged(_:)), for: .valueChanged)
        self.addSubview(progressSlider!)

        playButton = UIButton.init()
        playButton!.setImage(LSImageNamed(name: "暂停"), for: .normal)
        playButton!.setImage(LSImageNamed(name: "播放"), for: .selected)
        playButton!.addTarget(self, action: #selector(playAction(_:)), for: .touchUpInside)
        self.addSubview(playButton!)
        
        rateButton = UIButton.init()
        rateButton!.backgroundColor = .gray
        rateButton!.setTitle("1.0", for: .normal)
        rateButton!.setTitleColor(.white, for: .normal)
        rateButton!.layer.masksToBounds = true
        rateButton!.addTarget(self, action: #selector(rateAction(_:)), for: .touchUpInside)
        self.addSubview(rateButton!)

        fullScreenButton = UIButton.init()
        fullScreenButton!.setImage(LSImageNamed(name: "全屏"), for: .normal)
        fullScreenButton!.addTarget(self, action: #selector(fullScreenAction(_:)), for: .touchUpInside)
        self.addSubview(fullScreenButton!)

        if UIApplication.shared.statusBarOrientation == .portrait || UIApplication.shared.statusBarOrientation == .portraitUpsideDown {
            blackButton?.snp.makeConstraints({ make in
                make.top.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20) + LSNAVIGATION_BAR_HEIGHT())
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24)))
            })
            
            titleLabel?.snp.makeConstraints({ make in
                make.centerY.equalTo(blackButton!.snp.centerY)
                make.left.equalTo(blackButton!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 10))
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -70))
            })
            
            titleLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 18))
            timeLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 12))
            durationLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 12))
            rateButton?.layer.cornerRadius = getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20)
            
            timeLabel?.snp.makeConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -80) - bottomSafePadding)
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
            })

            durationLabel?.snp.makeConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -80) - bottomSafePadding)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -20))
            })
            
            progressSlider?.snp.makeConstraints({ make in
                make.centerY.equalTo(timeLabel!.snp.centerY)
                make.left.equalTo(timeLabel!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 10))
                make.right.equalTo(durationLabel!.snp.left).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -10))
                make.height.equalTo(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
            })
            
            playButton?.snp.makeConstraints({ make in
                make.top.equalTo(progressSlider!.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
                make.centerX.equalTo(self.snp.centerX)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24)))
            })

            rateButton?.snp.makeConstraints({ make in
                make.centerY.equalTo(self.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -20))
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 40), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 40)))
            })

            fullScreenButton?.snp.makeConstraints({ make in
                make.centerY.equalTo(playButton!.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -20))
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24)))
            })
        } else {
            blackButton?.snp.makeConstraints({ make in
                make.top.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20))
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 80) + leftSafePadding)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24)))
            })
            
            titleLabel?.snp.makeConstraints({ make in
                make.centerY.equalTo(blackButton!.snp.centerY)
                make.left.equalTo(blackButton!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 10))
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -100) + rightSafePadding)
            })
            
            titleLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 28))
            timeLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24))
            durationLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24))
            rateButton?.layer.cornerRadius = getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20)
            
            timeLabel?.snp.makeConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -60) - bottomSafePadding)
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 80) + leftSafePadding)
            })
            
            durationLabel?.snp.makeConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -60) - bottomSafePadding)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -80) - rightSafePadding)
            })
            
            progressSlider?.snp.makeConstraints({ make in
                make.centerY.equalTo(timeLabel!.snp.centerY)
                make.left.equalTo(timeLabel!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 10))
                make.right.equalTo(durationLabel!.snp.left).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -10))
                make.height.equalTo(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20))
            })
            
            playButton?.snp.makeConstraints({ make in
                make.top.equalTo(progressSlider!.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20))
                make.centerX.equalTo(self.snp.centerX)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24)))
            })
            
            rateButton?.snp.makeConstraints({ make in
                make.centerY.equalTo(self.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -80) - rightSafePadding)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 40), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 40)))
            })
            
            fullScreenButton?.snp.makeConstraints({ make in
                make.centerY.equalTo(playButton!.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -80) - rightSafePadding)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24)))
            })
        }
    }
    
    @objc func blackAction() {
        blackActionBlock?()
    }
    
    @objc func progressSliderValueChanged(_ slider: UISlider) {
        progressChangeBlock?()
    }
    
    @objc func playAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        playActionBlock?(sender.isSelected)
    }
    
    @objc func rateAction(_ sender: UIButton) {
        rateActionBlock?(sender)
    }
    
    @objc func fullScreenAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        fullScreenActionBlock?(sender.isSelected)
        updateFrame(withFull: sender.isSelected)
    }
    
    func getSYRealValue(withWidth width: CGFloat, isFull: Bool, isValue: CGFloat) -> CGFloat {
        var realValue: CGFloat = 0.0
        if isFull {
            //横屏
            realValue = isValue / 834.0 * width
        } else {
            //竖屏
            realValue = isValue / 375.0 * width
        }
        return realValue
    }
    
    func updateFrame(withFull isFull: Bool) {
        var leftSafePadding: CGFloat = 0.0
        var rightSafePadding: CGFloat = 0.0
        var bottomSafePadding: CGFloat = 0.0
        if #available(iOS 11.0, *) {
            leftSafePadding = self.safeAreaInsets.left
            rightSafePadding = self.safeAreaInsets.right
            bottomSafePadding = self.safeAreaInsets.bottom
        }
        let screenWidth = UIScreen.main.bounds.size.height
        
        if isFull == false {
            blackButton?.snp.updateConstraints({ make in
                make.top.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20) + LSNAVIGATION_BAR_HEIGHT())
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24)))
            })
            
            titleLabel?.snp.updateConstraints({ make in
                make.centerY.equalTo(blackButton!.snp.centerY)
                make.left.equalTo(blackButton!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 10))
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -70))
            })
            
            titleLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 18))
            timeLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 12))
            durationLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 12))
            rateButton?.layer.cornerRadius = getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20)
            
            timeLabel?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -80) - bottomSafePadding)
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
            })

            durationLabel?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -80) - bottomSafePadding)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -20))
            })
            
            progressSlider?.snp.updateConstraints({ make in
                make.centerY.equalTo(timeLabel!.snp.centerY)
                make.left.equalTo(timeLabel!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 10))
                make.right.equalTo(durationLabel!.snp.left).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -10))
                make.height.equalTo(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
            })
            
            playButton?.snp.updateConstraints({ make in
                make.top.equalTo(progressSlider!.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 20))
                make.centerX.equalTo(self.snp.centerX)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24)))
            })

            rateButton?.snp.updateConstraints({ make in
                make.centerY.equalTo(self.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -20))
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 40), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 40)))
            })

            fullScreenButton?.snp.updateConstraints({ make in
                make.centerY.equalTo(playButton!.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: false, isValue: -20))
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: false, isValue: 24)))
            })
        }else{
            blackButton?.snp.updateConstraints({ make in
                make.top.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20))
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 80) + leftSafePadding)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24)))
            })
            
            titleLabel?.snp.updateConstraints({ make in
                make.centerY.equalTo(blackButton!.snp.centerY)
                make.left.equalTo(blackButton!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 10))
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -100) + rightSafePadding)
            })
            
            titleLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 28))
            timeLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24))
            durationLabel?.font = LSSystemFont(NAME: "Regular", FONTSIZE: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24))
            rateButton?.layer.cornerRadius = getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20)
            
            timeLabel?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -60) - bottomSafePadding)
                make.left.equalTo(self).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 80) + leftSafePadding)
            })
            
            durationLabel?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -60) - bottomSafePadding)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -80) - rightSafePadding)
            })
            
            progressSlider?.snp.updateConstraints({ make in
                make.centerY.equalTo(timeLabel!.snp.centerY)
                make.left.equalTo(timeLabel!.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 10))
                make.right.equalTo(durationLabel!.snp.left).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -10))
                make.height.equalTo(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20))
            })
            
            playButton?.snp.updateConstraints({ make in
                make.top.equalTo(progressSlider!.snp.bottom).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 20))
                make.centerX.equalTo(self.snp.centerX)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24)))
            })
            
            rateButton?.snp.updateConstraints({ make in
                make.centerY.equalTo(self.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -80) - rightSafePadding)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 40), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 40)))
            })
            
            fullScreenButton?.snp.updateConstraints({ make in
                make.centerY.equalTo(playButton!.snp.centerY)
                make.right.equalTo(self.snp.right).offset(getSYRealValue(withWidth: screenWidth, isFull: true, isValue: -80) - rightSafePadding)
                make.size.equalTo(CGSize(width: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24), height: getSYRealValue(withWidth: screenWidth, isFull: true, isValue: 24)))
            })
        }
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
